package com.clothe.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.clothe.dao.IOrderDetialDAO;
import com.clothe.dto.Clothing;
import com.clothe.dto.OrderDetial;

public class OrderDetialDAOImpl implements IOrderDetialDAO {
	private Connection conn = null;

	@Override
	public void setConn(Connection conn) {
		this.conn = conn;
	}

	@Override
	public boolean save(OrderDetial orderDetial) throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "insert into orders_detial(order_id,clothe_id,clothing_name,amount,price) values(?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setLong(1, orderDetial.getOrderID());
			ps.setInt(2, orderDetial.getClotheID());
			ps.setString(3, orderDetial.getName());
			ps.setInt(4, orderDetial.getAmount());
			ps.setDouble(5, orderDetial.getPrice());
			if (ps.executeUpdate() > 0)
				flag = true;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	
	@Override
	public OrderDetial get(int id) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		OrderDetial orderDetial = null;
		Clothing clothing = null;
		try {
			String sql = "select * from order_view where id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				orderDetial = new OrderDetial();
				clothing = new Clothing();
				orderDetial.setOrderID(rs.getInt("order_id"));
				orderDetial.setClotheID(rs.getInt("clothe_id"));
				clothing.setName(rs.getString("clothing_name"));
				orderDetial.setAmount(rs.getInt("amount"));
				orderDetial.setPrice(rs.getDouble("price"));
				clothing.setSize(rs.getString("size"));
				clothing.setColor(rs.getString("color"));
				orderDetial.setClothing(clothing);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return orderDetial;
	}

	@Override
	public List<OrderDetial> list(int pageSize, int pageNo, long id)
			throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<OrderDetial> orderDetiallist = null;
		OrderDetial orderDetial = null;
		Clothing clothing = null;
		try {
			orderDetiallist = new ArrayList<OrderDetial>();
			String sql = "select * from order_view where order_id = ? limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			ps.setInt(2, (pageNo - 1) * pageSize);
			ps.setInt(3, pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				orderDetial = new OrderDetial();
				clothing = new Clothing();
				orderDetial.setOrderID(rs.getLong("order_id"));
				clothing.setName(rs.getString("clothing_name"));
				clothing.setSize(rs.getString("size"));
				clothing.setColor(rs.getString("color"));
				orderDetial.setClothing(clothing);
				orderDetial.setAmount(rs.getInt("amount"));
				orderDetial.setPrice(rs.getDouble("price"));
				orderDetiallist.add(orderDetial);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {

			rs.close();
			ps.close();
		}
		return orderDetiallist;
	}

	@Override
	public int getCount(long orderid) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = "select count(*) from order_view where order_id=?";
			ps = conn.prepareStatement(sql);
			ps.setLong(1, orderid);
			rs = ps.executeQuery();
			if (rs.next())
				return rs.getInt(1);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			rs.close();
			ps.close();
		}
		return 0;
	}

}
